package com.example.demo.cdesign.web;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 关闭Rserve进程
 * 目前为Linux系统
 */
public class CloseRserveProcess {
//    public static void main(String[] args) {
//        List<String> PIDS = CloseRserveProcess.getPID("Rserve");
//        System.out.println(Separator.listToString(PIDS,new Separator(" ")));
//        closeLinuxProcess(PIDS);
//    }
    public static class Separator {
        private static String next = "";
        private static String separator;

        public Separator(String separator) {
            this.separator = separator;
        }

        public static String get() {
            String result = next;
            next = separator;
            return result;
        }


        public static String listToString(List<String> list, Separator separator) {
            StringBuilder sb = new StringBuilder();
            for (String s : list) {
                if (s != null && !"".equals(s)) {
                    sb.append(separator.get()).append(s);
                }
            }
            return sb.toString();
        }
    }

    /**
     * 获取Linux进程的PID
     * @param command
     * @return
     */
    public static List<String> getPID(String command){
        BufferedReader reader =null;
        try{
            //显示所有进程
            Process process = Runtime.getRuntime().exec("ps -ef");
//            System.out.println("ps -ef|grep Rserve|grep -v grep");
//            Process process = Runtime.getRuntime().exec(command);

            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//            System.out.println(reader.readLine());
            List<String> ls = new ArrayList<>();
            String line = null;
            while((line = reader.readLine())!=null){
//                System.out.println(line);
                if(line.contains(command) && !line.contains("Java")){
//                    System.out.println("相关信息 -----> "+command);
//                    System.out.println(line);
                    String[] strs = line.split("\\s+");
//                    System.out.println(strs);
                    ls.add(strs[2]);
//                    System.out.println(ls);
                }
//                return ls;
            }
            return ls;
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {

                }
            }
        }
        return null;
    }

    /**
     * 关闭Linux进程
     * @param PIDS 进程的PID
     */
    public static boolean closeLinuxProcess(List<String> PIDS){
        Process process = null;
        BufferedReader reader =null;
        try{
            //杀掉进程
            process = Runtime.getRuntime().exec("kill -9 "+Separator.listToString(PIDS,new Separator(" ")));
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while((line = reader.readLine())!=null){
                System.out.println("kill PID return info -----> "+line);
            }
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally{
            if(process!=null){
                process.destroy();
            }

            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {

                }
            }
        }
    }
}